package com.clearliang.netmodule;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.clearliang.netmodule.api.ApiService;
import com.clearliang.netmodule.api.AppClient;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import okhttp3.ResponseBody;
import rx.Observable;
import rx.Subscriber;


/**
 * 测试界面
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    protected ApiService apiStores;
    private Map<String, String> map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = new HashMap<>();
        map.put("name", "tom");
        map.put("age", "15");
        map.put("sex", "man");

        initView();

    }

    private void initView() {
        apiStores = AppClient.retrofit().create(ApiService.class);

        btn1 = (Button) findViewById(R.id.btn_1);
        btn2 = (Button) findViewById(R.id.btn_2);
        btn3 = (Button) findViewById(R.id.btn_3);
        btn4 = (Button) findViewById(R.id.btn_4);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        btn4.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_1:
                break;
            case R.id.btn_2:
                NetUtil.getCookieUtil().test(new Subscriber<ResponseBody>() {
                    @Override
                    public void onCompleted() {
                        showLog("子——完成");
                    }

                    @Override
                    public void onError(Throwable e) {
                        showLog("子——错误");
                    }

                    @Override
                    public void onNext(ResponseBody responseBody) {
                        try {
                            showLog(responseBody.string());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
            case R.id.btn_3:
                break;
            case R.id.btn_4:
                break;
            default:
                break;
        }
    }

    private void showLog(String msg){
        Log.e("信息",msg);
    }
}
